<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SomeTools-点名</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <link href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link rel="stylesheet" href="jquery-3.4.1.js">
    <link rel="stylesheet" href="RoolCallstyle.css">
    <link rel="stylesheet" href="../font-awesome-4.7.0/css/font-awesome.css">
    <link rel="stylesheet" href="../font-awesome-4.7.0/css/font-awesome.min.css">
    <link rel='icon' href='../image/ICO.ico' type='image/x-ico' />

</head>

<body>
    <button id="soundContorl" onclick="soundContorl()" class="btn btn-light">
        <i class="fa fa-volume-up fa-lg"></i>
    </button>
    <audio id="startMusic">
        <source src="./抽奖背景音乐.mp3" loop="loop" type="audio/mpeg">
    </audio>
    <audio id="stopMusic">
        <source src="./醉赤壁 - 林俊杰.mp3" type="audio/mpeg">
    </audio>
    <audio id="nullMusic">
    </audio>
    <p id="roolName">点击开始</p>
    <!-- 按钮：用于打开模态框 -->
    <div id="btns">
        <button type="button" class="btn btn-light" data-toggle="modal" data-target="#myModal">
            添加…
        </button>
        <button type="button" class="btn btn-light" onclick="collCall()">开始</button>
        <button type="button" class="btn btn-light" onclick="stop()">停止</button>
    </div>

    <!-- 模态框 -->
    <div class="modal fade" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">
                <!-- 模态框头部 -->
                <div class="modal-header">
                    <h4 class="modal-title">添加被点名人员</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <!-- 模态框主体 -->
                <div class="modal-body">
                    <textarea type="text" rows="10" id="names" class="form-control" placeholder="张三
李四
王五
…………
依次换行
                    "></textarea>
                    <div class="container mt-3">
                        <div class="custom-file mb-6">
                            <input oninput="checkFile()" accept="text/plain" type="file" class="custom-file-input" id="customFile" name="filename">
                            <label class="custom-file-label" id="filePath" for="customFile">或者选择文件……</label>
                        </div>
                    </div>
                </div>

                <!-- 模态框底部 -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" onclick="fullArr()" data-dismiss="modal">确定</button>
                </div>

            </div>
        </div>
    </div>
    <script>
        let nameArr;
        //alert(nameArr);

        let is = false;
        let startMusic = document.getElementById('startMusic');
        let play = true; //是否播放
        let start = false;

        function checkFile() {
            let selectFile = document.getElementById('customFile').files[0];
            let path = $("#customFile").val();
            $("#filePath").html(path);
            let reader = new FileReader(); //这是核心,读取操作就是由它完成.
            let text = reader.readAsText(selectFile); //读取文件的内容,也可以读取文件的URL
            reader.onload = function() {
                //当读取完成后回调这个函数,然后此时文件的内容存储到了result中,直接操作即可
                $("#names").html(this.result);
            }
        }

        function fullArr() {
            nameArr = $("#names").val().split("\n");
        }


        function collCall() {
            if (nameArr != undefined) {
                start = true;
                if (play) {
                    startMusic.play();
                }
                is = false;

                for (let i = 0; i < nameArr.length; i++) {
                    setInterval(() => {
                        if (is) {
                            return;
                        }
                        $("#roolName").html(nameArr[i]);
                    }, 1);
                }
            }


        }



        function stop() {
            if (nameArr != undefined) {
                start = false;
                startMusic.pause();
                is = true;
            }

        }



        function soundContorl() {
            if (play) {
                $("#soundContorl").html('<i class="fa fa-volume-off fa-lg"></i>');
                play = false;
                if (start) {
                    startMusic.pause();
                }
            } else {
                $("#soundContorl").html('<i class="fa fa-volume-up fa-lg"></i>');
                play = true;
                if (start) {
                    startMusic.play();
                }

            }
        }
    </script>
</body>

</html>